﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using WeiXin.WeChatData;
using System.Data;

namespace WeiXin
{
    public class MessageHandler
    {
        public static string WxNum = "";

        /// <summary>
        /// 群列表
        /// </summary>
        public static List<ChatRoom> weChatRoomList = new List<ChatRoom>();

        /// <summary>
        /// 好友列表
        /// </summary>
        public static List<UserInfo> weChatFriends = new List<UserInfo>();

        /// <summary>
        /// 当前登录用户
        /// </summary>
        public static UserInfo myUserInfo = new UserInfo();

        /// <summary>
        /// 分割消息用字符串
        /// </summary>
        public static char[] separator = new char[] { '|', '|' };


        private static object lockObj = new object();
      
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static void Parse10003(object data)
        {
            string message = (string)data;
            message = message.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
            if (message.Length > 0)
            {
                string[] info = message.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                if (info.Length >= 4)
                {
                    myUserInfo.UserName = info[0];
                    myUserInfo.WeixinNum = info[1];
                    WxNum = info[1];
                    myUserInfo.WeixinId = info[2];
                    myUserInfo.WeixinLogo = info[3];
                }
            }
        }

        /// <summary>
        /// 获取群列表
        /// </summary>
        /// <param name="data"></param>
        public static void Parse10006(object data)
        {
            string message = (string)data;
            AnalysisCharRoom(message);
        }
        /// <summary>
        /// 解析群信息
        /// </summary>
        /// <param name="dataStr"></param>
        /// <returns></returns>
        private static ChatRoom AnalysisCharRoom(string dataStr)
        {
            dataStr = dataStr.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
            string[] info = dataStr.Split(MessageHandler.separator, StringSplitOptions.RemoveEmptyEntries);
            string roomid = info[0];
            string roomname = info[1];
            if (roomid.StartsWith("="))
            {
                return null;
            }
            ChatRoom room = new ChatRoom();
            room.RoomId = roomid;
            room.RoomName = roomname;


            #region 保存到群组列表
            lock (lockObj)
            {
                ChatRoom chatRoom = MessageHandler.weChatRoomList.FirstOrDefault(x => x.RoomId == room.RoomId);
                if (chatRoom != null && !string.IsNullOrWhiteSpace(chatRoom.RoomId))
                {
                    chatRoom.RoomName = room.RoomName;
                }
                else
                {
                    LogHelper.WriteLog("AnalysisCharRoom:" + "||" + JsonConvert.SerializeObject(room));
                    MessageHandler.weChatRoomList.Add(room);
                }
            }


            #endregion
            return room;
        }


        /// <summary>
        /// 邀请加入群组
        /// </summary>
        /// <param name="data"></param>
        public static void Parse10013(object data)
        {

        }
     
        public static string GetStandardDate(string str)
        {
            string[] arr = str.Split(new char[] { '-' });
            string month = arr[1];
            string day = arr[2].Substring(0, arr[2].IndexOf(":"));

            if (month.Length == 1)
            {
                month = "0" + month;
            }
            if (day.Length == 1)
            {
                day = "0" + day;
            }

            string[] times = arr[2].Split(new char[] { ':' });
            string hour = times[1];
            string mm = times[2];
            string ss = times[3];
            string hhh = times[4];

            if (hour.Length == 1)
            {
                hour = "0" + hour;
            }
            if (mm.Length == 1)
            {
                mm = "0" + mm;
            }
            if (ss.Length == 1)
            {
                ss = "0" + ss;
            }
            return arr[0] + "-" + month + "-" + day + ":" + hour + ":" + mm + ":" + ss + ":" + hhh;
        }

     
    }
}
